package com.jerry.controller;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * Created by Shirly on 2016/8/18.
 */
@Aspect
public class AnnotationAspectAdvice {
    @Pointcut("execution(* com.jerry.controller..*.*(..))")
    public void anyMethod() {
    }

    /**
     * 前置通知
     *
     * @param jp
     */
    @Before(value = "execution(* com.jerry.controller..*.*(..))")
    public void doBefore(JoinPoint jp) {
        System.out.println("===========进入before advice============ \n");

        System.out.print("准备在" + jp.getTarget().getClass() + "对象上用");
        System.out.print(jp.getSignature().getName() + "方法进行对 '");
        System.out.print(jp.getArgs()[0] + "'进行删除！\n\n");

        System.out.println("要进入切入点方法了 \n");
    }

    /**
     * 后置通知
     *
     * @param jp
     *            连接点
     *            返回值
     */
    @AfterReturning(value="anyMethod()")
    public void doAfter(JoinPoint jp) {
        System.out.println("==========进入after advice=========== \n");
        System.out.println("切入点方法执行完了 \n");

        System.out.print(jp.getArgs()[0] + "在");
        System.out.print(jp.getTarget().getClass() + "对象上被");
        System.out.print(jp.getSignature().getName() + "方法删除了");
        System.out.print("只留下：" + "\n\n");
    }

    /**
     * 环绕通知
     *
     * @param pjp
     *            连接点
     */
    @Around(value = "execution(* com.jerry.controller..*.*(..))")
    public void doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("===========进入around环绕方法！=========== \n");

        // 调用目标方法之前执行的动作
        System.out.println("调用方法之前: 执行！\n");

        // 调用方法的参数
        Object[] args = pjp.getArgs();
        // 调用的方法名
        String method = pjp.getSignature().getName();
        // 获取目标对象
        Object target = pjp.getTarget();
        // 执行完方法的返回值：调用proceed()方法，就会触发切入点方法执行
         pjp.proceed();

        System.out.println("输出：" + args[0] + ";" + method + ";" + target + ";" + "\n");
        System.out.println("调用方法结束：之后执行！\n");
    }

    /**
     * 异常通知
     *
     * @param jp
     * @param e
     */
    @AfterThrowing(value = "execution(* com.jerry.controller.*.*(..))", throwing = "e")
    public void doThrow(JoinPoint jp, Throwable e) {
        System.out.println("删除出错啦");
      //  System.out.println(e);
    }

    public static void main(String[] args) {
        Integer a= null;
        System.out.println(a!=null & a>0);
    }
}
